//
//  ScanViewController.m
//  Demo54_QRCodeScan
//
//  Created by 李海龙 on 15/10/28.
//  Copyright © 2015年 北京千锋互联科技有限公司（大连）. All rights reserved.
//

#import "ScanViewController.h"

@interface ScanViewController ()

@end

@implementation ScanViewController
{
    NSTimer *timer;
    UIView *sao;
    float h;
    BOOL up;
}
//初始化相机
- (void)viewDidLoad {
    [super viewDidLoad];
    h=5;
    up = YES;
    [self startcamera];
    [self creatUI];
}

- (void)creatUI
{
    
    
    
    //设置扫描区域屏幕上方遮罩
    UIView *updown1 = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*0.2, 0, self.view.frame.size.width*0.6, self.view.frame.size.height*0.3)];
    updown1.backgroundColor = [UIColor grayColor];
    updown1.alpha = 0.4;
    [self.view addSubview:updown1];
    //设置扫描区域屏幕下方遮罩
    UIView *updown2 = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*0.2, self.view.frame.size.height*0.3+self.view.frame.size.width*0.6, self.view.frame.size.width*0.6, self.view.frame.size.height-self.view.frame.size.height*0.3)];
    updown2.backgroundColor = [UIColor grayColor];
    updown2.alpha = 0.4;
    [self.view addSubview:updown2];
    
    //设置扫描区域屏幕左边遮罩
    UIView *leftright1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width*0.2, self.view.frame.size.height)];
    leftright1.backgroundColor = [UIColor grayColor];
    leftright1.alpha = 0.4;
    [self.view addSubview:leftright1];
    //设置扫描区域屏幕右边遮罩
    UIView *leftright2 = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*0.8, 0, self.view.frame.size.width*0.2, self.view.frame.size.height)];
    leftright2.backgroundColor = [UIColor grayColor];
    leftright2.alpha = 0.4;
    [self.view addSubview:leftright2];
    
    
    //设置扫描区域上边线
    UIView *heng1 = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*0.2, self.view.frame.size.height*0.3, self.view.frame.size.width*0.6, 3)];
    heng1.backgroundColor = [UIColor greenColor];
    [self.view addSubview:heng1];
    //设置扫描区域下边线
    UIView *heng2 = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*0.2, self.view.frame.size.height*0.3+self.view.frame.size.width*0.6, self.view.frame.size.width*0.6, 3)];
    heng2.backgroundColor = [UIColor greenColor];
    [self.view addSubview:heng2];
    
    //设置扫描区域左边线
    UIView *shu1 = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*0.2, self.view.frame.size.height*0.3, 3, self.view.frame.size.width*0.6)];
    shu1.backgroundColor = [UIColor greenColor];
    [self.view addSubview:shu1];
    //设置扫描区域右边线
    UIView *shu2 = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*0.8, self.view.frame.size.height*0.3, 3, self.view.frame.size.width*0.6+3)];
    shu2.backgroundColor = [UIColor greenColor];
    [self.view addSubview:shu2];
    
    
    //设置扫描区滚动条
    sao = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*0.2, self.view.frame.size.height*0.3, self.view.frame.size.width*0.6, 3)];
    sao.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:sao];
    //设置扫描区滚动条滚动
    timer = [NSTimer scheduledTimerWithTimeInterval:0.08 target:self selector:@selector(fly) userInfo:nil repeats:YES];
    
    
    UILabel *promptLB = [[UILabel alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height*0.7, self.view.frame.size.width, 20)];
    promptLB.text = @"将二维码放入扫描区域内即可完成扫描";
    promptLB.textColor = [UIColor whiteColor];
    promptLB.textAlignment = NSTextAlignmentCenter;
    promptLB.font  =[UIFont systemFontOfSize:12];
    [self.view addSubview:promptLB];
    
}
//设置扫描框内滚动条规律晃动
- (void)fly
{

    if (up) {
        sao.center = CGPointMake(self.view.frame.size.width/2, sao.center.y+5);
        if (sao.center.y >= self.view.frame.size.height*0.3+self.view.frame.size.width*0.6) {
            up = NO;
        }
    }
    else
    {
        sao.center = CGPointMake(self.view.frame.size.width/2, sao.center.y-5);
        if (sao.center.y <= self.view.frame.size.height*0.3) {
            up = YES;
        }
    }
}

//初始化相机
- (void)startcamera
{
    // Device
    
    _device = [ AVCaptureDevice defaultDeviceWithMediaType : AVMediaTypeVideo ];
    
    // Input
    
    _input = [ AVCaptureDeviceInput deviceInputWithDevice : self . device error : nil ];
    
    // Output
    
    _output = [[ AVCaptureMetadataOutput alloc ] init ];
    
    [ _output setMetadataObjectsDelegate : self queue : dispatch_get_main_queue ()];
    
    // Session
    
    _session = [[ AVCaptureSession alloc ] init ];
    
    [ _session setSessionPreset : AVCaptureSessionPresetHigh ];
    
    if ([ _session canAddInput : self . input ])
        
    {
        
        [ _session addInput : self . input ];
        
    }
    
    if ([ _session canAddOutput : self . output ])
        
    {
        
        [ _session addOutput : self . output ];
        
    }
    
    // 条码类型 AVMetadataObjectTypeQRCode
    
    _output . metadataObjectTypes = @[ AVMetadataObjectTypeQRCode ] ;
    
    // Preview
    
    _preview =[ AVCaptureVideoPreviewLayer layerWithSession : _session ];
    
    _preview . videoGravity = AVLayerVideoGravityResizeAspectFill ;
    
    _preview . frame = self . view . layer . bounds ;
    
    [ self . view . layer insertSublayer : _preview atIndex : 0 ];
    
    //设置非扫描区域内不能扫描
    //1.这个属性的参数范围都是0-1
    //2.测试发现，这个参数里德x对应的时距离左上角的垂直距离，y对应的是距离左上角的水平距离。高度和宽度类似
    //3.经验就是，将扫描区域的frame的前两个参数调换位置，后两个参数分别除以对角线屏宽
    _output.rectOfInterest = CGRectMake(0.3, 0.2, self.view.frame.size.width*0.6/self.view.frame.size.height, 0.6);
    // Start
    
    //开始扫描
    [ _session startRunning ];

}

//处理扫描的结果
- ( void )captureOutput:( AVCaptureOutput *)captureOutput didOutputMetadataObjects:( NSArray *)metadataObjects fromConnection:( AVCaptureConnection *)connection

{
    
    NSString *stringValue;
    
    if ([metadataObjects count ] > 0 )
        
    {
        
        
        
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
        //返回扫描数据的字符串
        stringValue = metadataObject. stringValue ;        
        
    }
    
    // 停止扫描
    
    [ _session stopRunning ];
    
    //让扫动线停止
    sao.hidden = YES;
    //[timer invalidate];
    
    NSLog(@"StringValue is %@", stringValue);
    NSLog(@"我好像扫到了一些东西");
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:stringValue]];
    
    //设置0.3秒后扫描页面退回至前页面
    [self performSelector:@selector(wait) withObject:self afterDelay:0.3];
    
}
- (void)wait
{
    [self.navigationController popViewControllerAnimated:NO];
}

@end
